package com.kern.controller;

import com.alibaba.fastjson.JSON;
import com.kern.api.pojo.Dept;
import com.kern.common.base.ResponseResult;
import com.kern.common.util.RpcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  消费者ctl
 * Created by caoshi at 21:24 2021-09-04
 */
@RestController
public class DeptConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    public static final String REST_URL_SUFFIX = "http://localhost:8001";

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") String id) {
        System.out.println("id = " + id);
        return restTemplate.getForObject(REST_URL_SUFFIX + "/dept/get/" + id, Dept.class);
    }

    @RequestMapping("/consumer/dept/testPost")
    public Dept testPost() {
        // 根据服务ID 获取服务实例
        List<ServiceInstance> instances = discoveryClient.getInstances("demoprovider");
        ServiceInstance service = instances.get(0);
        String host = service.getHost();
        int port = service.getPort();

//        System.out.println("host = " + host + "\n port = " + port);
//        String serverName = host + ":" + port + "/dept/givePost";
//        System.out.println("serverName " + serverName);

        String serverName = "demoprovider/dept/givePost";

        Map<String, Object> args = new HashMap<>();

        args.put("name", "zs");
        args.put("age", 19);
        args.put("man", true);

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> formEntity = new HttpEntity<>(JSON.toJSONString(args), headers);
        Dept dept = restTemplate.postForObject("http://" + serverName, formEntity, Dept.class);
        System.out.println("dept " + dept);
        return dept;
    }

    /**
     * 消费者
     *  测试 RpcUtil.callService 方法
     * @return
     */
    @RequestMapping("/consumer/dept/giveRpc")
    public ResponseResult testRpcUtil() {
        String serverName = "demoprovider/dept/giveRpc";
        Map<String, Object> args = new HashMap<>();
        args.put("name", "张三");
        args.put("age", 19);
        args.put("man", true);
        // 调用远程方法
        ResponseResult result = RpcUtil.callService(serverName, args);
        System.out.println("远程调用返回结果" + result);
        return result;
    }




}
